package com.wcity.manage.web.register;

import com.wcity.manage.common.bean.RetBean;
import com.wcity.manage.common.context.Constants;
import com.wcity.manage.common.enums.Code;
import com.wcity.manage.common.exception.CustomException;
import com.wcity.manage.common.utils.JSONUtil;
import com.wcity.manage.common.utils.StringUtil;
import com.wcity.manage.service.RegisterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by jn on 2016/12/10.
 * 注册用户相关接口
 */
@Controller
public class IRegisterAct {


    /**
     * 注册
     * @param phone
     * @param code
     * @param pwd
     * @param cpwd
     * @return
     */
    @RequestMapping("register")
    @ResponseBody
    public RetBean register(String phone,String code,String pwd,String cpwd){

        if ( !StringUtil.isNumeric( phone, 11 ) || !StringUtil.isNumeric( code, 6 )
                || StringUtil.isBlank( pwd ) || StringUtil.isBlank( cpwd )
                || pwd.length() < 8 || !pwd.equals( cpwd ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.register( phone, code, pwd, cpwd );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "注册成功" );
    }

    /**
     * 重置密码
     * @param phone
     * @param code
     * @param pwd
     * @return
     */
    @RequestMapping("reset_pwd")
    @ResponseBody
    public RetBean resetPwd(String phone,String code,String pwd){

        if ( !StringUtil.isNumeric( phone, 11 ) || !StringUtil.isNumeric( code, 6 ) ||
                StringUtil.isBlank( pwd ) || pwd.length() < 8 )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.resetPwd( phone, code, pwd );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "重置成功" );
    }

    /**
     * 登陆
     * @param phone
     * @param pwd
     * @return
     */
    @RequestMapping("register_login")
    @ResponseBody
    public Map<String,Object> registerLogin(String phone,String pwd){

        if ( !StringUtil.isNumeric( phone, 11 ) || StringUtil.isBlank( pwd )
                || pwd.length() < 8 )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object errorMsg = registerService.login( phone, pwd );

        if ( errorMsg instanceof String ) throw new CustomException( (String) errorMsg, Code.ERROR );

        Map<String,Object> map = JSONUtil.getSuccessJson();
        map.put("loginInfo",errorMsg);
        return map;
    }

    /**
     * 修改密码
     * @param registerId
     * @param pwd
     * @param cpwd
     * @return
     */
    @RequestMapping("v1/update_pwd")
    @ResponseBody
    public RetBean updatePwd(String registerId,String pwd,String cpwd){

        if ( !StringUtil.isNumeric( registerId ) || StringUtil.isBlank( pwd )
                || StringUtil.isBlank( cpwd ) || pwd.length() < 8 || cpwd.length() < 8 )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.updatePwd( registerId, pwd, cpwd );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "修改成功" );
    }

    /**
     * 更新个人信息
     * @param nickname
     * @param icon
     * @param registerId
     * @return
     */
    @RequestMapping("v1/update_info")
    @ResponseBody
    public RetBean updateInfo(String nickname,String icon, String registerId){

        if ( !StringUtil.isNumeric( registerId ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        if ( !StringUtil.isBlank( nickname ) ) {
            if ( nickname.length() > 20 ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        }

        if ( !StringUtil.isBlank( icon ) ) {
            if ( icon.length() > 100 ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );
        }

        String errorMsg = registerService.updateInfo( registerId, nickname, icon );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "修改成功" );
    }

    /**
     * 更改手机号验证
     * @param registerId
     * @param code
     * @return
     */
    @RequestMapping("v1/bind_verify")
    @ResponseBody
    public Object bindVerify(String registerId,String code){

        if ( !StringUtil.isNumeric( registerId ) || !StringUtil.isNumeric( code, 6 ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object res = registerService.bindVerify( registerId, code );

        if ( res instanceof String ) throw new CustomException( (String)res, Code.ERROR );

        return res;

    }

    /**
     * 修改绑定手机号
     * @param registerId
     * @param verifyCode
     * @param newPhone
     * @param code
     * @return
     */
    @RequestMapping("v1/bind_change")
    @ResponseBody
    public RetBean bindChange(String registerId,String verifyCode,String newPhone,String code){

        if ( StringUtil.isBlank( verifyCode ) || !StringUtil.isNumeric( newPhone, 11 )
                || !StringUtil.isNumeric( code, 6 ) || !StringUtil.isNumeric( registerId ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.updateBindPhone( registerId, verifyCode, newPhone, code );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "更改成功" );
    }

    /**
     * 获取注册用户个人信息
     * @param registerId
     * @return
     */
    @RequestMapping("v1/register_info")
    @ResponseBody
    public Map<String,Object> registerInfo(String registerId){

        if ( !StringUtil.isNumeric( registerId ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        Object errorMsg = registerService.registerInfo( registerId );

        if ( errorMsg instanceof String ) throw new CustomException( (String)errorMsg, Code.ERROR );

        Map<String,Object> map = JSONUtil.getSuccessJson();
        map.put("info",errorMsg);
        return map;
    }

    /**
     * 完善个人信息
     * @param registerId
     * @param building
     * @param unit
     * @param house
     * @return
     */
    @RequestMapping("v1/complete_info")
    @ResponseBody
    public RetBean completeInfo(String registerId,String building,String unit,String house){

        if ( StringUtil.isBlank( building ) || StringUtil.isBlank( unit )
                || StringUtil.isBlank( house ) || !StringUtil.isNumeric( registerId )
                || building.length() > 10 || unit.length() > 10 || house.length() > 10 )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.completeInfo( registerId, building, unit, house );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "修改成功" );
    }

    /**
     * 登出
     * @param registerId
     * @return
     */
    @RequestMapping("v1/register_logout")
    @ResponseBody
    public RetBean registerLogout(String registerId){

        if ( !StringUtil.isNumeric( registerId ) ) throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.logout( registerId );
        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "登出成功" );
    }

    /**
     * 重置密码，修改绑定手机号获取验证码
     * @param registerId
     * @param phone 输入手机号
     * @param type 2重置密码 3修改绑定手机号第一步验证码 4修改绑定手机号第二步验证码
     * @return
     */
    @RequestMapping("v1/register_get_code")
    @ResponseBody
    public RetBean registerGetCode(String registerId, String phone, String type){

        if ( !StringUtil.isNumeric( registerId ) || !StringUtil.isNumeric( phone, 11 )
                || !( "2".equals( type ) || "3".equals( type ) || "4".equals( type ) ) )
            throw new CustomException( Constants.PARAMETER_ERROR, Code.ERROR );

        String errorMsg = registerService.registerGetCode( registerId, phone, type );

        if ( !StringUtil.isBlank( errorMsg ) ) throw new CustomException( errorMsg, Code.ERROR );

        return new RetBean( Code.SUCCESS, "获取成功" );
    }

    @Resource private RegisterService registerService;

}
